<?php
class ControllerLocalisationReturnAction extends AdminController {

  	public function index() {
		M('localisation/return_action');
    	$this->getList();
  	}

  	public function insert() {
    	M('localisation/return_action');

		if ($this->request->isPost() && $this->validateForm()) {
      		$this->model_localisation_return_action->addReturnAction($this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
      		$this->redirect(UA('localisation/return_action'));
		}

    	$this->getForm();
  	}

  	public function update() {
		M('localisation/return_action');

    	if ($this->request->isPost() && $this->validateForm()) {
	  		$this->model_localisation_return_action->editReturnAction($this->request->get['return_action_id'], $this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
      		$this->redirect(UA('localisation/return_action'));
    	}

    	$this->getForm();
  	}

  	public function delete() {
		M('localisation/return_action');

    	if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $return_action_id) {
				$this->model_localisation_return_action->deleteReturnAction($return_action_id);
			}

			$this->session->set_flashdata('success', $this->_['text_success']);
      		$this->redirect(UA('localisation/return_action'));
   		}

    	$this->getList();
  	}

  	protected function getList() {
		$this->document->setTitle($this->_['heading_title']);

		$qf = new Query_filter();

		$return_action_total = $this->model_localisation_return_action->getTotalReturnActions();
		$this->data['return_actions'] = $this->model_localisation_return_action->getReturnActions($qf);

		$this->data['pagination'] = $this->pagination($return_action_total)->render();

		$this->display('localisation/return_action_list.tpl');
  	}

  	protected function getForm() {
		$this->document->setTitle($this->_['heading_title']);

		if (isset($this->request->get['return_action_id'])) {
			$this->data['action'] = UA('localisation/return_action/update', 'return_action_id=' . $this->request->get['return_action_id']);
			if (!$this->request->isPost()) {
				$this->vo->data['return_action'] = $this->model_localisation_return_action->getReturnActionDescriptions($this->request->get['return_action_id']);
			}
		} else {
			$this->data['action'] = UA('localisation/return_action/insert');
		}

		$this->data['languages'] = C('cache_language_2');

		$this->display('localisation/return_action_form.tpl');
  	}

	protected function validateForm() {

		$pass = true;

    	foreach ($this->request->post['return_action'] as $language_id => $value) {
      		if ((utf8_strlen($value['name']) < 3) || (utf8_strlen($value['name']) > 32)) {
        		$this->setMessage('error_name_' . $language_id, $this->_['error_name']);
				$pass = false;
      		}
    	}

		return $pass;
  	}

  	protected function validateDelete() {

		M('sale/return');

		foreach ($this->request->post['selected'] as $return_action_id) {
			$return_total = $this->model_sale_return->getTotalReturnsByReturnActionId($return_action_id);

			if ($return_total) {
	  			$this->setMessage('error_warning', sprintf($this->_['error_return'], $return_total));
				return false;
			}
	  	}

		return true;
  	}
}
?>